package com.meizi.mall.product.utils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.meizi.core.tool.constant.ProductConstant;
import org.apache.commons.lang.StringUtils;

import java.util.Map;

/**
 * @author meizi
 * @email enjoymeizi@gmail.com
 * @date 2021/1/11 12:03
 */
public class ProjectQueryWapper<T> {
    public QueryWrapper<T> productQueryAttrByCommon(Map<String, Object> params, String attrType, Long categoryId, String filedId, String filedName) {
        QueryWrapper<T> queryWrapper = new QueryWrapper<>();
        if (attrType != null) {
            queryWrapper.eq("attr_type",
                    "base".equalsIgnoreCase(attrType) ? ProductConstant.AttrEnum.ATTR_TYPE_BASE.getCode() :
                            ProductConstant.AttrEnum.ATTR_TYPE_SALE.getCode());
        }
        queryWrapper.eq("catelog_id", categoryId);
        String key = (String) params.get("key");
        if (StringUtils.isNotBlank(key)) {
            queryWrapper.and((wrapper) -> {
                wrapper.eq(filedId, key).or().like(filedName, key);
            });
        }
        return queryWrapper;
    }
}
